<template>
  <div>
    <!-- Header 区域 -->
    <van-nav-bar
      fixed
      title="文章详情"
      left-arrow
      @click-left="$router.back()"
    />
    <!-- <van-loading type="spinner" color="#1989fa" v-if="biaoti===undefined">玩命加载中。。。</van-loading> -->
    <van-loading type="spinner" color="#1989fa" v-if="artObj.title===undefined">玩命加载中。。。</van-loading>
    <!-- 文章信息区域 -->
    <div class="article-container">
      <!-- 文章标题 -->
      <h1 class="art-title">{{ biaoti }}</h1>

      <!-- 用户信息 -->
      <van-cell center :title="zuozhe" label="3天前">
        <template #icon>
          <img :src="artObj.aut_photo" alt="" class="avatar" />
        </template>
        <template #default>
          <div>
            <van-button
              type="info"
              size="mini"
              v-if="artObj.is_followed"
              @click="followFn(false)"
              >已关注</van-button
            >
            <van-button
              icon="plus"
              type="info"
              size="mini"
              plain
              v-else
              @click="followFn(true)"
              >关注</van-button
            >
          </div>
        </template>
      </van-cell>

      <!-- 分割线 -->
      <van-divider></van-divider>

      <!-- 文章内容 -->
      <div class="art-content" v-html="wenzhangneirong"></div>

      <!-- 分割线 -->
      <van-divider>End</van-divider>

      <!-- 点赞 -->
      <div class="like-box">
        <van-button
          icon="good-job"
          type="danger"
          size="small"
          v-if="artObj.attitude != -1"
          @click="likeFn('no')"
          >不喜欢</van-button
        >
        <van-button
          icon="good-job-o"
          type="danger"
          plain
          size="small"
          v-else
          @click="likeFn('yes')"
          >喜欢</van-button
        >
      </div>
    </div>
    <!-- 文章评论区域 -->
    <!-- 原本是$router.XXXXX 路由传值-->
    <CommentList
      :artId="this.artObj.art_id"
      v-if="this.artObj.art_id"
    ></CommentList>
  </div>
</template>

<script>
import CommentList from './CommentList' // 引入评论组件
import {
  articleDetailApi,
  guanzhuzuozheApi,
  quguanzuozheApi,
  likeApi,
  dislikeApi
} from '@/api'
export default {
  name: 'ArticleDetail',
  components: {
    CommentList
  },

  async created () {
    const res = await articleDetailApi({
      aid: this.$route.query.aid
    })
    console.log(res)
    this.wenzhangneirong = res.data.data.content
    this.zuozhe = res.data.data.aut_name
    this.biaoti = res.data.data.title
    this.artObj = res.data.data
  },
  methods: {
    // 是否关注作者的函数
    async followFn (bool) {
      // bool === true代表用户点的是关注按钮
      if (bool === true) {
        this.artObj.is_followed = true
        // console.log(this.$route.query.aid)
        console.log(this.artObj)
        await guanzhuzuozheApi({ aid: this.artObj.aut_id }) // 这里的参数是作者ID
      } else {
        this.artObj.is_followed = false
        await quguanzuozheApi({ aid: this.artObj.aut_id })
      }
    },
    // 是否喜欢文章的函数
    async likeFn (i) {
      // 如果i是'yes'，证明点击的是喜欢，让不喜欢显示，发送喜欢的请求
      if (i === 'yes') {
        this.artObj.attitude = 1
        await likeApi({
          aid: this.artObj.art_id // 这里的参数是文章ID
        })
      } else {
        this.artObj.attitude = -1
        await dislikeApi({
          aid: this.artObj.art_id // 这里的参数是文章ID
        })
      }
    }
  },
  data () {
    return {
      wenzhangneirong: '', // 文章内容
      zuozhe: '', // 作者
      biaoti: '', // 标题
      artObj: {} // 获取到的数据对象
    }
  }
}
</script>

<style scoped lang="less">
.article-container {
  padding: 10px;
  margin-top: 46px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}
.van-loading{
  padding-top: 1.2888888rem;
  text-align: center;
}
</style>
